package com.xpalive.config.spring.command;

import com.xpalive.ymlconfig.CustomCnf;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
@Slf4j
public class UploadCommandLineRunner implements CommandLineRunner {

    private final CustomCnf customCnf;
    private final String defPath = "upload" + File.separator + "tmp";

    @Override
    public void run(String... args) throws Exception {
        String uploadTmpDir = Optional.ofNullable(customCnf.getUploadTmpDir()).orElse(defPath);
        Path path = Paths.get(uploadTmpDir);
        if (Files.exists(path)) {
            log.info("tmp dir is exists {}", path.toAbsolutePath().toString());
        } else {
            Files.createDirectories(path);
            log.info("tmp dir is created {}", path.toAbsolutePath().toString());
        }
    }
}
